package subject_set.lc200;

/**
 * @author haomin
 * @date 2022/12/28 22:40
 **/
public class Num28 {
    class Solution {
        public int strStr(String haystack, String needle) {
            String n2 = haystack.replace(needle, "1");
            for(int i = 0; i < n2.length(); ++i) {
                if(n2.charAt(i) != haystack.charAt(i)) return i;
            }
            return -1;
        }
    }

    class Solution2 {
        public int strStr(String haystack, String needle) {
            if(needle.length() > haystack.length()) return -1;
            for(int i = 0; i < haystack.length(); ++i) {
                int l = i, j = 0;
                if(haystack.charAt(i) == needle.charAt(0)) {
                    while(l < haystack.length() && j < needle.length() && haystack.charAt(l) == needle.charAt(j)) {
                        l++; j++;
                    }
                    if(j == needle.length()) return i;
                }
            }
            return -1;
        }
    }
}